package com.dchh.android.suverynew.module.local.impl;

import android.content.Context;

import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dchh.android.suverynew.module.local.ITableSelectMoudle;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.Node;
import com.dhcc.android.base.common.bean.Table;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.dataparse.DataParse;
import com.dhcc.android.base.utils.dataparse.NodeParse;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by eric on 2016/8/13.
 */
public class TableSelectMoudle implements ITableSelectMoudle {
    private TableSelectMoudleListener moudleListener;

    public TableSelectMoudle(TableSelectMoudleListener moudleListener) {
        this.moudleListener = moudleListener;
    }

    @Override
    public void initData(InputStream inputStream, String periodCode) {
        List<Table> tlist = DataParse.tableParse(inputStream, periodCode);
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; i < tlist.size(); i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", tlist.get(i).getCode());
            map.put("name", tlist.get(i).getLabel());
            list.add(map);
        }
        this.moudleListener.onInitData(list);
    }

    @Override
    public void restore() {
        boolean ef = SPreferencesManager.getValue(MSApplication.EXCEPTION_FLAG, false);
        if (ef) {
            String code = SPreferencesManager.getValue(MSApplication.TABLE_CODE, "");
            String label = SPreferencesManager.getValue(MSApplication.TABLE_LABEL, "");

            Table tb = new Table();
            tb.setCode(code);
            tb.setLabel(label);
            this.moudleListener.restore(tb);
        }
    }

    @Override
    public void getNodeList(String root, String code) {
        // 回去配置文件路径
        String p = root + "/" + Constants.currentTask.getTitle() + "/DEMAND/C_TASK.xml";
        List<Node> nodeList = null;
        try {
            InputStream is = new FileInputStream(p);
            // 解析TASK.xml文件
            nodeList = NodeParse.parseNodeList(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 设为全局静态常量
        Constants.nodelist = nodeList;
        this.moudleListener.onNodeList(code);
    }

    @Override
    public void initYCDatas(String root, String tbid, String tblabel, String periodLabel, Context context) {
// 判断是否已经提取数据
        String yc_dbpath = root + "/" + Constants.currentTask.getTitle() + "/TASK/YIELD/DATA.db";
        String mj_dbpath = root + "/" + Constants.currentTask.getTitle() + "/TASK/TRANSPORT/DATA.db";
        DBManagerUtil yc_dbutil = DBManagerUtil.getInstance(yc_dbpath, context);
        int count = yc_dbutil.getCount(yc_dbpath, "预产表", null, null);
        if (count > 0) {
            this.moudleListener.onGoToActivity(tbid, tblabel);
        } else {
            this.moudleListener.onYCDataSeaching();
            DBManagerUtil mj_dbutil = DBManagerUtil.getInstance(mj_dbpath, context);
            String zwtable = "作物" + Constants.currentTask.getRefrence();
            String sstable = "设施" + Constants.currentTask.getRefrence();
            List<Map<String, String>> list = mj_dbutil.getResearchDatas(zwtable, sstable, mj_dbpath);
            if (list != null && list.size() > 0) {
                System.out.println("-预产-面积调查数据--：" + list.size());
                yc_dbutil.saveResearchDatasFORYC(periodLabel, tblabel, list, "预产表", yc_dbpath);
                moudleListener.onYCData(tbid, tblabel);
            } else {
                this.moudleListener.onYCDataEmpty();
            }
        }
    }

    public interface TableSelectMoudleListener {
        void onInitData(List<Map<String, String>> list);

        void restore(Table table);

        void onNodeList(String code);

        void onGoToActivity(String tbid, String tblabel);

        void onYCDataSeaching();

        void onYCData(String tbid, String tblabel);

        void onYCDataEmpty();
    }
}
